#region File information

/**
 * File: WorldData.cs
 * Date: 11-03-2008
 * Auth: Jan-Jaap Severs
 * 
 * Desc: A WorldData describes all game entity states that need to be replicated across the network,
 *       from server to clients, at an update.
 */

#endregion

#region Using statements

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.GamerServices;

#endregion

namespace Squadron.Networking
{
    /// <summary>
    /// A WorldData packet describes all game entity states that need to be replicated across the network,
    /// from server to clients, at an update.
    /// </summary>
    class WorldData
    {
        #region Header information

        /// This packet's type.
        PacketType packetType = PacketType.WorldData;
        
        /// The number of ships in the game world.
        UInt16 numShips;

        #endregion

        #region Ship information

        List<Vector3> shipPositions;
        List<Quaternion> shipRotations;
        List<UInt16> shipIDs;

        #endregion        

        public WorldData(UInt16 numShips, List<Vector3> shipPositions, List<Quaternion> shipRotations, 
            List<UInt16> shipIDs)
        {
            this.numShips = numShips;
            this.shipPositions = shipPositions;
            this.shipRotations = shipRotations;
            this.shipIDs = shipIDs;
        }

        /// <summary>
        /// Send a SquadronPacket to all the clients.
        /// </summary>
        /// <param name="packetWriter"></param>
        /// <param name="sendDataOptions"></param>
        /// <param name="server"></param>
        public void Write(PacketWriter packetWriter, SendDataOptions sendDataOptions, LocalNetworkGamer server)
        {   
            // Write the header
            packetWriter.Write((int)packetType);
            packetWriter.Write(numShips);

            for(UInt16 ship = 0; ship < numShips; ship++)
            {
                packetWriter.Write(shipIDs[ship]);
                packetWriter.Write(shipPositions[ship]);
                packetWriter.Write(shipRotations[ship]);
            }

            // Send the data across the network.
            server.SendData(packetWriter, sendDataOptions);
        }

        /// <summary>
        /// Read SquadronPacket from the network stream.
        /// </summary>
        /// <param name="gamer"></param>
        /// <param name="packetReader"></param>
        public static WorldData Read(LocalNetworkGamer gamer, PacketReader packetReader)
        {
            List<Vector3> shipPositions = new List<Vector3>();
            List<Quaternion> shipRotations = new List<Quaternion>();
            List<UInt16> shipIDs = new List<UInt16>();

            // Read the header info from the sent packet.
            UInt16 numShips = packetReader.ReadUInt16();

            for(UInt16 ship = 0; ship < numShips; ship++)
            {
                UInt16 shipID = packetReader.ReadUInt16();
                Vector3 position = packetReader.ReadVector3();
                Quaternion rotation = packetReader.ReadQuaternion();

                shipIDs.Add(shipID);
                shipPositions.Add(position);
                shipRotations.Add(rotation);
            }

            return new WorldData(numShips, shipPositions, shipRotations, shipIDs);
        }

        public UInt16 NumShips
        {
            get { return numShips; }
        }

        public List<UInt16> IDs
        {
            get { return shipIDs; }
        }

        public List<Vector3> Positions
        {
            get { return shipPositions; }
        }

        public List<Quaternion> Rotations
        {
            get { return shipRotations; }
        }
    }
}
